//
//  HaoWudakaViewController.m
//  HaoWuEnglish
//
//  Created by 暗流澎涌 on 2024/5/19.
//

#import "HaoBaClickkaVC.h"
#import <Photos/Photos.h>
#import "SVProgressHUD.h"
@interface HaoBaClickkaVC ()

@property (weak, nonatomic) IBOutlet UIImageView *deleteImage;

@property (weak, nonatomic) IBOutlet UIView *hwPicView;

@property (weak, nonatomic) IBOutlet UILabel *desLabel;


@end

@implementation HaoBaClickkaVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
    [self HBdesTitle];
}
-(void)creatUI{
    UITapGestureRecognizer* HBtaps = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clcikImage)];
    [self.deleteImage setUserInteractionEnabled:YES];
    [self.deleteImage addGestureRecognizer:HBtaps];
   
}
-(void)HBdesTitle{
    NSDateFormatter *Format = [[NSDateFormatter alloc] init];
    Format.dateFormat = @"EEE,MM dd";
    self.desLabel.text = [Format stringFromDate:[NSDate now]];
}
-(void)clcikImage{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)HaobashardImage:(id)sender {
    UIActivityViewController *Shard = [[UIActivityViewController alloc] initWithActivityItems:@[[self HaoBagetImage]] applicationActivities:nil];
    [self presentViewController:Shard animated:YES completion:nil];
    
}
- (IBAction)HaoBastogImage:(id)sender {
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (status == PHAuthorizationStatusAuthorized) {
               
                [self haobaDownImageV];
            } else {
                UIAlertController *tips = [UIAlertController alertControllerWithTitle:@"提示" message:@"请开启相册权限" preferredStyle:UIAlertControllerStyleAlert];
                [tips addAction:[UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
                }]];
                [tips addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
                [self presentViewController:tips animated:YES completion:nil];
            }
        });
    }];
}
-(void)haobaDownImageV{
    UIImage *currImage = [self HaoBagetImage];
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            [PHAssetChangeRequest creationRequestForAssetFromImage:currImage];
    } completionHandler:^(BOOL success, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (success) {
                [SVProgressHUD showSuccessWithStatus:@"保存成功"];
            } else {
                [SVProgressHUD showErrorWithStatus:@"保存失败"];
            }
        });
        
    }];
}
- (UIImage*)HaoBagetImage{
    UIGraphicsBeginImageContextWithOptions(self.hwPicView.bounds.size, NO, UIScreen.mainScreen.scale);
    [self.hwPicView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *imageinfo = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return imageinfo;
}
@end
